Java program to check if the given number is Prime?
Java program to check if the given number is Prime?
687
16-Nov-2021
Updated on 18-Apr-2023
Krishnapriya Rajeev
23-Mar-2023Given below is the code to check if a given number is prime or not.
Mukul Goenka
17-Nov-2021public class PrimeNumberCheck { public static void main(String[] args) { System.out.println(isPrime(19)); // true System.out.println(isPrime(49)); // false } public static boolean isPrime(int n) { if (n == 0 || n == 1) { return false; } if (n == 2) { return true; } for (int i = 2; i <= n / 2; i++) { if (n % i == 0) { return false; } } return true; } }Output.
true
false